package com.my.ads.lib.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

import com.my.ads.lib.Config;
import com.my.ads.lib.activity.JunkCleanActivity;
import com.my.ads.lib.activity.MemoryBoostActivity;
import com.my.ads.lib.utils.Utils;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by xuanyang on 2018/3/27.
 */

public class AppReceiver extends BroadcastReceiver {

    private String appTarget = "appTime";
    private String appKey = "appKey";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(Config.TagLog, "app");
        try {
            SharedPreferences sp = context.getSharedPreferences(appTarget, MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            long now = System.currentTimeMillis();
            long time = sp.getLong(appKey, 0L);
            if (Math.abs(now - time) < 15 * 1000) {
                return;
            } else {
                editor.putLong(appKey, now);
                editor.commit();
            }
            if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
                Utils.startActivity(context, JunkCleanActivity.class);
            } else {
                Utils.startActivity(context, MemoryBoostActivity.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
